
#include <BPainter>
#include <BRender>
#include <BViewer>
#include <BPhysics>
#include <BCone>
#include <BCube>
#include <BReadWrite>
#include <BImage>

#include "DynamicScene.h"
#include "PointHandler.h"

PointHandler::PointHandler()
{
	_nodeA = new BNode("nodeA");
	{
		BPhysics* physics = new BPhysics();
		physics->setMass(3.0);
		_nodeA->setPhysics(physics);

		BCube* cube = new BCube(5, 12);
		cube->setColor(100, 200, 255);
		_nodeA->addShape(cube);
	}

	_nodeB = new BNode("nodeB");
	{
		BPhysics* physics = new BPhysics();
		physics->setMass(1.0);
		_nodeB->setPhysics(physics);

		BImage* image = ReadFile<BImage>("data/image/whitemetal_diffuse.jpg");

		BCone* cone = new BCone(0.5, 2, 24, 10);
		cone->setColor(200, 100, 0);
		cone->setTexture(image);
		_nodeB->addShape(cone);
	}

	BVector pointA = _nodeA->space().max();
	BVector pointB(0, 0, _nodeB->space().max().z());
	_pointConstraint = new BPointConstraint(_nodeA, pointA, _nodeB, pointB);
	_pointConstraint->setName("Point");
}
PointHandler::~PointHandler()
{

}

void PointHandler::attached(BViewer* viewer)
{
	DynamicScene* dscene = (DynamicScene*)viewer->scene();
	_nodeA->setPosition(0, 0, 13);
	_nodeB->setPosition(2, 2, 13);
	dscene->insert(_nodeA);
	dscene->insert(_nodeB);
	dscene->insert(_pointConstraint);
	this->emit("edit-constraint", _pointConstraint);
}
void PointHandler::detached(BViewer* viewer)
{
	DynamicScene* dscene = (DynamicScene*)viewer->scene();
	dscene->remove(_nodeA);
	dscene->remove(_nodeB);
	dscene->remove(_pointConstraint);
}

void PointHandler::paint(BPainter& painter)
{
	BViewer* viewer = this->viewer();
	painter.setLineWidth(10);
	painter.setColor(BColor(0, 1, 0));
	painter.drawRect(10, 10, viewer->width() - 20, viewer->height() - 20);
}
void PointHandler::render(BRender& render)
{
	render.setLighting(false);
	render.setDepthTest(false);
	render.setLineSmooth(true);
	render.setLineWidth(1);
	
}
